package web.bio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Client {
	
	private static String ip="127.0.0.1";
	private static int port=7777;
	public static void main(String[] args) {
		
		for (int i = 0; i < 1024; i++) {
			try {
				Socket socket=new Socket(ip, port);
				new Thread(new ClientHandle(socket),"zsw"+i).start();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

class ClientHandle implements Runnable{
	
	private Socket socket;
	

	public ClientHandle(Socket socket) {
		super();
		this.socket = socket;
	}


	@Override
	public void run() {
		

		BufferedReader in=null;
		BufferedWriter out=null;
		try {
		 in=new BufferedReader(new InputStreamReader(socket.getInputStream()));	
		 out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		 out.write("hello"+Thread.currentThread().getName());
		 out.flush();
		socket.shutdownOutput();
		String s=null;
		while((s=in.readLine())!=null) {
			System.out.println("client -->我是客户端收到服务端的信息："+s);
		}
		socket.shutdownInput();
		
		
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(in!=null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				in=null;
			}
			if(out!=null) {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				out=null;
			}
			
			if(socket!=null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				socket=null;
			}
		}
		
	
	
	}
	
}
